﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Rescue_of_the_Dwarvenzombies.Scene.Levels;
using Rescue_of_the_Dwarvenzombies.SceneObjects;
using Rescue_of_the_Dwarvenzombies.SceneObjects.SceneObjectsHandlers;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Characters.Enemies;
using GameLibrary;

namespace Rescue_of_the_Dwarvenzombies.SceneLayers
{
    /// <summary>
    /// The EnemyLayer holds all the enemies in a list so that they get the same depth value when being
    /// drawed and so that the AI can treat it as a whole.
    /// </summary>
    /// <remarks></remarks>
    internal class EnemyLayer : SceneLayer
    {
        /// <summary>
        /// The EnemyLayer needs to be associtated wh
        /// </summary>
        private EnemyHandler enemyHandler;
        /// <summary>
        /// 
        /// </summary>
        private Game _game;

        /// <summary>
        /// Initializes a new instance of the <see cref="EnemyLayer"/> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <remarks></remarks>
        public EnemyLayer(Game game)
            : base(game)
        {
            _game = game;
            ItemLayer itemLayer = new ItemLayer(game);
            Child = itemLayer;
            Depth = Constant.ENEMY_LAYER_DEPTH;
            enemyHandler = new EnemyHandler(game, itemLayer.ItemHandler);
            LayerName = Constant.Layer.Enemy;
            InitObjects();
            AI.Instance().EnemyLayer = this;
        }

        /// <summary>
        /// Inits the objects.
        /// </summary>
        /// <remarks></remarks>
        protected override sealed void InitObjects()
        {
            InitSpawnPositions();
        }

        /// <summary>
        /// Gets the scene objects.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public override List<ISceneObject> GetSceneObjects()
        {
            SceneObjects = enemyHandler.GetCurrentEnemies();
            return SceneObjects;
        }

        /// <summary>
        /// Inits the spawn positions.
        /// </summary>
        /// <remarks></remarks>
        private void InitSpawnPositions()
        {
            Vector2[] spawningPos = ((ILevel)_game.Services.GetService(typeof(ILevel))).GetSpawningPos();
            int amountOfEnemies = ((ILevel)_game.Services.GetService(typeof(ILevel))).GetAmountOfEnemies();
            SceneObjects = enemyHandler.MakeNewWave(amountOfEnemies, 0, spawningPos);
        }
    }
}